package cn.basic.util;

import cn.basic.enums.ReturnCodeEnum;
import cn.basic.exception.DONotAlarmException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/2/24 15:25
 */
public class ValidationUtil {

    public static void validate(BindingResult bindingResult) throws Exception{
        if (bindingResult.hasErrors()) { // 参数解析错误
            List<ObjectError> errList = bindingResult.getAllErrors();
            StringBuilder errMsg = new StringBuilder();
            for (ObjectError oe : errList) {
                errMsg.append(oe.getDefaultMessage()).append("，");
            }
            throw new DONotAlarmException(ReturnCodeEnum.PARAMETER_ERROR.getCode(),errMsg.toString());
        }
    }
}
